package com.firestorm.eagletrons;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import com.firestorm.eagletrons.official.R;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class PreferencesActivity extends PreferenceActivity {
	GoogleAnalyticsTracker tracker;
	Boolean logged_in;
	SharedPreferences settings;
	SharedPreferences.Editor editor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.startNewSession("UA-25025598-9", this);
		tracker.trackPageView("/" + getClass().getSimpleName());

		addPreferencesFromResource(R.xml.preference);
		Preference pref_rate = (Preference) findPreference("preference_rate");
		Preference pref_account = (Preference) findPreference("preference_account");
		Preference pref_goback = (Preference) findPreference("preference_navigation_goback");
		Preference pref_logout = (Preference) findPreference("preference_account_logout");
		Preference pref_version = (Preference) findPreference("preference_version");
		Preference pref_reset = (Preference) findPreference("preference_reset");
		CharSequence version;
		try {
			version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
			pref_version.setSummary(version);
		} catch (Exception e) {

		}
		settings = getSharedPreferences("eagletrons", 0);
		editor = settings.edit();
		logged_in = settings.getBoolean("logged_in", false);
		if (logged_in) {
			pref_account.setEnabled(false);
		} else {
			pref_logout.setEnabled(false);
		}

		pref_account
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						try {
							tracker.trackEvent("Clicks", // Category
									"Preferences/Account_Pressed", // Action
									"Preferences/Account_Pressed", // Label
									1); // Value
							Intent account_intent = new Intent(
									getApplicationContext(),
									com.firestorm.eagletrons.login.LoginActivity.class);
							startActivity(account_intent);
						} catch (Exception e) {

						}
						return true;
					}
				});

		pref_logout
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						try {
							tracker.trackEvent("Clicks", // Category
									"Preferences/Account_Logout_Pressed", // Action
									"Preferences/Account_Logout_Pressed", // Label
									1); // Value

							editor.putBoolean("logged_in", false);
							editor.commit();
							finish();
						} catch (Exception e) {
							e.printStackTrace();
						}
						return true;
					}
				});

		pref_rate.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			public boolean onPreferenceClick(Preference preference) {
				try {
					tracker.trackEvent("Clicks", // Category
							"Preferences/Rate_Pressed", // Action
							"Preferences/Rate_Pressed", // Label
							1); // Value
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(Uri
							.parse("market://details?id=com.firestorm.eagletrons"));
					startActivity(intent);
				} catch (Exception e) {

				}
				return true;
			}
		});

		pref_reset
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						try {
							tracker.trackEvent("Clicks", // Category
									"Preferences/Reset_Pressed", // Action
									"Preferences/Reset_Pressed", // Label
									1); // Value
							Context activity = getApplicationContext();
							SharedPreferences settings = PreferenceManager
									.getDefaultSharedPreferences(activity);
							Editor editor = settings.edit();
							editor.clear();
							editor.commit();
							kill_activity();

						} catch (Exception e) {

						}
						return true;
					}
				});

		pref_goback
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {
					public boolean onPreferenceClick(Preference preference) {
						finish();
						return true;
					}
				});

	}

	public void kill_activity() {
		this.finish();
	}

	@Override
	public void onPause() {
		super.onPause();
		tracker.dispatch();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		tracker.stopSession();
	}

}